﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ProjectWebNhac.DAO;
namespace ProjectWebNhac.BUS
{
    public class TheLoaiBus
    {
        public TheLoaiBus() { }
        Dataprovider dao = new Dataprovider();
        public string LayTheLoaiTheoCaSi(int MaCasi)
        {
            DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayTheLoaiTheoCasi", CommandType.StoredProcedure,
                new object[] { "@MaCaSi",MaCasi });
            string TheLoai = "";
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    TheLoai += row["TenTheLoai"].ToString() + " ,";
                }
                TheLoai = TheLoai.Remove(TheLoai.Length - 1);
            }
            else
                TheLoai = "Đang Cập Nhật";
            return TheLoai;
        }

        public List<int> LayMaTheLoaiTheoTen(string tenTheLoai)
        {
            List<int> lsMaTheLoai = new List<int>();
            List<string> lstenTheLoai = tenTheLoai.Split(',').ToList();
            foreach (var item in lstenTheLoai)
            {
                //item.Replace(" ", "");
                item.TrimStart();
                item.TrimEnd();
                DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayMaTheLoaiTheoTen", CommandType.StoredProcedure,
                new object[] { "@Ten", item });
                if (dt.Rows.Count != 0)
                {
                    lsMaTheLoai.Add(Convert.ToInt16(dt.Rows[0][0]));
                }
                else
                {
                    ThemTheLoai(item);
                    lsMaTheLoai.Add(Convert.ToInt16(dao.LayDuLieuThamSo("sp_Admin_LayMaTheLoaiTheoTen", CommandType.StoredProcedure,
                    new object[] { "@Ten", item }).Rows[0][0]));
                }
            }
            return lsMaTheLoai;
        }


        public bool ThemTheLoai(string TenTheLoai)
        {
            return dao.XuLySuKien("sp_Admin_ThemTheLoai", CommandType.StoredProcedure,
                new object[] { "@TenTheLoai", TenTheLoai });
        }
        public DataTable LayTheLoaiNhac()
        {
            return dao.LayDuLieuThamSo("sp_Admin_LoadTheLoaiNhac", CommandType.StoredProcedure);
        }
        //public int LayMaTheLoaiTheoTenInt(string TenTheLoai)
        //{
        //    DataTable dt = dao.LayDuLieuThamSo("sp_Admin_LayMaTheLoaiTheoTen", CommandType.StoredProcedure,
        //        new object[] { "@Ten",TenTheLoai });
        //    if (dt.Rows.Count>0)
        //    {
        //        return Convert.ToInt16(dt.Rows[0][0]);
        //    }
        //    else
        //    {
        //        ThemTheLoai(TenTheLoai);
        //        return Convert.ToInt16(dao.LayDuLieuThamSo("", CommandType.StoredProcedure,
        //            new object[] { "@Ten", TenTheLoai }).Rows[0][0]);
        //    }
        //}
    }
}